/*
 *  Copyright 2005 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.action.core.browser;

import com.blandware.atleap.webapp.action.core.grid.BaseGridAction;
import com.blandware.atleap.webapp.util.core.WebappConstants;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * <p>Base class for browser actions.</p>
 * <p><a href="BaseBrowserAction.java.html"><i>View source</i></a></p>
 *
 * @author Roman Puchkovskiy <a href="mailto:roman.puchkovskiy@blandware.com">
 *         &lt;roman.puchkovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.3 $ $Date: 2007/12/19 12:49:48 $
 */
abstract class BaseBrowserAction extends BaseGridAction {

    /**
     * Returns whether this is opening.
     *
     * @param request request
     * @return whether this is opening
     */
    protected boolean getOpening(HttpServletRequest request) {
        return "true".equalsIgnoreCase(request.getParameter("opening"));
    }

    /**
     * Processes opening and returns calculated mode. If opening is true,
     * locale suffix is removed from session. Calculated mode is saved into
     * session.
     *
     * @param opening whether this is opening
     * @param session session
     * @param openingMode mode that will be set if opening is true
     * @param fallbackMode mode that will be set of opening is false and mode
     * stored in session is not valid
     * @return mode
     */
    protected String processOpeningAndGetMode(boolean opening,
                                              HttpSession session,
                                              String openingMode,
                                              String fallbackMode) {
        String mode;
        if (opening) {
            session.removeAttribute(WebappConstants.BROWSER_LOCALE_SUFFIX_KEY);
            mode = openingMode;
        } else {
            mode = (String) session.getAttribute(WebappConstants.BROWSER_MODE_KEY);
            if (!WebappConstants.BROWSER_MODE_LINKS.equalsIgnoreCase(mode)
                    && !WebappConstants.BROWSER_MODE_PAGE_LINKS.equalsIgnoreCase(mode)
                    && !WebappConstants.BROWSER_MODE_RESOURCE_LINKS.equalsIgnoreCase(mode)
                    && !WebappConstants.BROWSER_MODE_IMAGES.equalsIgnoreCase(mode)
                    && !WebappConstants.BROWSER_MODE_FILES.equalsIgnoreCase(mode)) {
                mode = fallbackMode;
            }
        }
        session.setAttribute(WebappConstants.BROWSER_MODE_KEY, mode);
        return mode;
    }

    /**
     * Computes menu name on mode and saves it into request.
     *
     * @param mode mode
     * @param request request
     */
    protected void computeMenuNameAndSaveToRequest(String mode,
                                                   HttpServletRequest request) {
        String browserMenuName = null;
        if (WebappConstants.BROWSER_MODE_LINKS.equalsIgnoreCase(mode)) {
            browserMenuName = WebappConstants.BROWSER_LINKS_MENU_NAME;
        } else if (WebappConstants.BROWSER_MODE_PAGE_LINKS.equalsIgnoreCase(mode)) {
            browserMenuName = WebappConstants.BROWSER_PAGE_LINKS_MENU_NAME;
        } else if (WebappConstants.BROWSER_MODE_RESOURCE_LINKS.equalsIgnoreCase(mode)) {
            browserMenuName = WebappConstants.BROWSER_RESOURCE_LINKS_MENU_NAME;
        } else if (WebappConstants.BROWSER_MODE_IMAGES.equalsIgnoreCase(mode)) {
            browserMenuName = WebappConstants.BROWSER_IMAGES_MENU_NAME;
        } else if (WebappConstants.BROWSER_MODE_FILES.equalsIgnoreCase(mode)) {
            browserMenuName = WebappConstants.BROWSER_FILES_MENU_NAME;
        }
        request.setAttribute("browserMenuName", browserMenuName);
    }
}
